package com.quanyan.club.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.vo.req.ReqClubCommentPage;
import com.quanyan.club.entity.vo.resp.RespClubComment;
import com.quanyan.club.entity.vo.resp.RespClubMark;
import com.quanyan.club.service.ClubCommentService;
import com.quanyan.common.Plugin.PageObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 16/7/18.
 */
@RestController
@RequestMapping(value = "/api/club/comment",method = RequestMethod.POST)
public class ClubCommentController {
    @Autowired
    private ClubCommentService clubCommentService;

    /**
     * 获取俱乐部评分
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "getClubMark")
    public String getClubMark(@RequestBody Map map,HttpServletRequest request){
        if (!map.containsKey("clubId")) {
            return JSONObject.toJSONString(APIResponse.returnFail("请传入必要参数"));
        }
        int clubId = (int) map.get("clubId");
        RespClubMark respClubMark = this.clubCommentService.getClubMark(clubId,0);
        APIResponse apiResponse = APIResponse.returnSuccess(respClubMark);
        return JSONObject.toJSONString(apiResponse,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    /**
     * 获取俱乐部评论列表
     * @param reqClubCommentPage
     * @return
     */
    @RequestMapping(value = "list")
    public String getClubCommentList(@RequestBody ReqClubCommentPage reqClubCommentPage){
        PageObj<List<RespClubComment>> respClubCommentObj = this.clubCommentService.getClubCommentList(reqClubCommentPage);
        if(respClubCommentObj == null){
            return JSONObject.toJSONString(APIResponse.returnFail("评论列表获取失败"));
        }else{
            return JSONObject.toJSONString(APIResponse.returnSuccess(respClubCommentObj),
                    SerializerFeature.WriteNullBooleanAsFalse,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty);
        }
    }

}
